package com.zjuee.web.ledger.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.dto.common.plan.PlanSolidDto;
import com.zjuee.base.model.common.plan.PlanSolid;
import com.zjuee.service.PlanSolidService;
import com.zjuee.web.ledger.mapper.PlanSolidMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ding jinli
 * @since 1.0
 */
@Slf4j
@Service
public class PlanSolidServiceImpl implements PlanSolidService {
    @Resource
    PlanSolidMapper planSolidMapper;

    @Override
    public Long add(PlanSolid type) {
        int state = planSolidMapper.insert(type);
        if (1 == state) {
            return type.getId();
        }
        return null;
    }

    @Override
    public boolean delete(Long id, Long validateId) {
        PlanSolid planSolid = planSolidMapper.selectById(id);
        if (validateId.equals(planSolid.getWpiId())) {
            int type = planSolidMapper.deleteById(id);
            if (1 == type) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean update(PlanSolid type) {
        int state = planSolidMapper.updateById(type);
        if (1 == state) {
            return true;
        }
        return false;
    }

    @Override
    public PlanSolid selectById(Long id, Long validateId) {
        PlanSolid planSolid = planSolidMapper.selectById(id);
        if (validateId.equals(planSolid.getWpiId())) {

            return planSolid;
        }
        return null;
    }

    @Override
    public Page<PlanSolid> list(PlanSolidDto dto) {
        return null;
    }

    @Override
    public LambdaQueryWrapper<PlanSolid> fuzzyQuery(PlanSolidDto dto) {
        return null;
    }

    @Override
    public List<PlanSolid> finSolidListBywpiId(Long wpiId, Integer wpsType) {
        if (wpiId == null || wpsType == null) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("wpi_id", wpiId);
        queryWrapper.eq("wps_type", wpsType);
        return planSolidMapper.selectList(queryWrapper);
    }

    @Override
    public List<String> getCodeBywpiId(Long wpiId, String wpsType) {
        Map<String, Object> map = new HashMap<>();
        map.put("wpi_id", wpiId);
        map.put("wps_model", 1);
        map.put("wps_type", wpsType);
        List<String> list = new ArrayList<>();
        for (PlanSolid planSolid : planSolidMapper.selectByMap(map)) {
            list.add(planSolid.getWpsCode());
        }
        return list;
    }

    @Override
    public List<PlanSolid> getPlanSolid(PlanSolid planSolid) {
        if (planSolid == null) {
            return null;
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        if (planSolid.getWpiId() != null) {
            queryWrapper.eq("wpi_id", planSolid.getWpiId());
        }
        if (planSolid.getWpsModel() != null) {
            queryWrapper.eq("wps_model", planSolid.getWpsModel());
        }
        if (planSolid.getWpsType() != null) {
            queryWrapper.eq("wps_type", planSolid.getWpsType());
        }
        if (planSolid.getWpsCode() != null) {
            queryWrapper.eq("wps_code", planSolid.getWpsCode());
        }

        return planSolidMapper.selectList(queryWrapper);
    }
}
